package cn.cboard.ajax;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;


public class ServiceLocator extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse resposne) throws ServletException{
		try {
			Context initial = new InitialContext();
			Context myEnv = (Context) initial.lookup("java:comp/env");
			Object ref = myEnv.lookup("dataSoruce/mysql-ds");
			//System.out.println(ref);
			
			DataSource ds = (DataSource)ref;
			try {
				Connection c = ds.getConnection();
				
				String sql = "select * from threadline";
				
				PreparedStatement ps = c.prepareStatement(sql);
				ResultSet rs = ps.executeQuery();
				
				while(rs.next()){
					System.out.println(rs.getString("title"));
				}
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
				
	}

	public void doPost(HttpServletRequest request, HttpServletResponse resposne) throws ServletException{
		
	}	
}